﻿using System.Collections.Generic;
using System.Linq;

namespace Quail
{
    /// <summary>
    /// Used in the HasYSOD method
    /// </summary>
    public class YSODSearchItem
    {
        private static readonly List<YSODSearchItem> _genericYSODs = new List<YSODSearchItem>();

        /// <summary>
        /// Initializes a new instance of the <see cref="YSODSearchItem"/> class.
        /// </summary>
        /// <param name="triggertext">The triggertext.</param>
        /// <param name="message">The message.</param>
        public YSODSearchItem(string triggertext, string message)
        {
            TriggerText = triggertext;
            TriggerTextLower = triggertext.ToLower();
            Message = message;
        }

        /// <summary>
        /// Gets the YSOD trigger text
        /// </summary>
        public string TriggerText
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the YSOD trigger text in lower case
        /// </summary>
        public string TriggerTextLower
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the YSOD message to display to the user
        /// </summary>
        public string Message
        {
            get;
            private set;
        }

        /// <summary>
        /// Returns an enumeration of generic YSOD search item objects
        /// </summary>
        /// <returns>The default list of known YSOD page contents</returns>
        public static IEnumerable<YSODSearchItem> GenericYSODSearchItems()
        {
            lock (_genericYSODs)
            {
                if (_genericYSODs.Count == 0)
                {
                    _genericYSODs
                        .AddRange(new[]
                                      {
                                          new YSODSearchItem("Server Error in '", "YSOD"),
                                          new YSODSearchItem("Oh no! We had an error!", "Error Page"),
                                          new YSODSearchItem("Page not found!", "404"),
                                          new YSODSearchItem("The service is temporarily unavailable.",
                                                             "Service YSOD"),
                                          new YSODSearchItem("My MVC Application", "MVCYSOD"),
                                          new YSODSearchItem("An error has occurred!", "TODO Message"),
                                          new YSODSearchItem("A PHP Error was encountered", "PHP YSOD"),
                                          new YSODSearchItem("We're sorry but an error occurred.", "Atlas Error Page"),
                                          new YSODSearchItem("Oh No! Page Not Found!", "EComm WordPress YSOD")
                                      });

                }
            }

            return _genericYSODs;
        }

        /// <summary>
        /// Adds a default YSOD search item to the list
        /// </summary>
        /// <param name="item">A YSOD search item</param>
        public static void AddYSODSearchItem(YSODSearchItem item)
        {
            lock (_genericYSODs)
            {
                GenericYSODSearchItems();

                if (!_genericYSODs.Exists(i => i.TriggerText == item.TriggerText))
                    _genericYSODs.Add(item);
            }
        }
    }


}
